<template>
  <div class="login-wrapper">
    <div class="login-box">
      <!-- 头像 -->
      <div class="login-avatar">
        <img src="@/assets/logo.png" alt />
      </div>

      <!-- 登录表单 -->
      <el-form
        class="login-form"
        ref="loginFormRef"
        :model="studentLoginForm"
        :rules="studentLoginFormRules"
      >
        <h3 class="title">学生登录</h3>
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            v-model="studentLoginForm.username"
            placeholder="请输入学号"
            prefix-icon="iconfont iconicon"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            v-model="studentLoginForm.password"
            placeholder="请输入密码"
            @keyup.enter.native="login"
            prefix-icon="iconfont iconmima"
            type="password"
          ></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="button">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="registerNo">注册账号</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      // 登录表单的对象
      studentLoginForm: {
        username: "",
        password: "",
      },
      studentLoginFormRules: {
        username: [
          { required: true, message: "请输入账号", trigger: "blur" },
          {
            min: 3,
            max: 12,
            message: "长度在 5 到 12 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 3,
            max: 15,
            message: "长度在 6 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    registerNo() {
      // 使用Vue Router进行路由跳转
      this.$router.push('/student/register');
    },

    login() {
      // 表单预验证
      this.$refs.loginFormRef.validate((valid) => {
        if (!valid) return;
        this.$axios
          .post("http://localhost:8080/student/login", {
            username: this.studentLoginForm.username,
            password: this.studentLoginForm.password,
          })
          .then((res) => {
            if (res.data.code == 0) {
              // 成功响应,得到token
              let ret = res.data.data;
              window.localStorage.setItem("token", ret.token);
              window.localStorage.setItem(
                "student",
                JSON.stringify(ret.student)
              );
              this.$router.push("/student");
              this.$message({ message: "登录成功", type: "success" });
            } else {
              alert(res.data.message);
            }
          })
          .catch((error) => {
            // 失败
            this.$message.error("登录失败");
          });
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login-wrapper {
  background: url('../images/studentbg.png') no-repeat center center;
  background-size: cover;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  
  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.3);
  }
}

.login-box {
  width: 450px;
  height: 380px;
  background-color: rgba(255, 255, 255, 0.95);
  border-radius: 15px;
  position: relative;
  box-shadow: 0 0 30px rgba(0, 0, 0, 0.2);
  backdrop-filter: blur(10px);
  z-index: 1;
}

.login-avatar {
  height: 120px;
  width: 120px;
  border: 2px solid #fff;
  border-radius: 50%;
  padding: 8px;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
  z-index: 2;
  
  img {
    height: 100%;
    width: 100%;
    border-radius: 50%;
    background-color: #f5f5f5;
  }
}

.login-form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 30px;
  box-sizing: border-box;
}

.title {
  color: #333;
  text-align: center;
  margin: 60px 0 30px;
  font-size: 24px;
  font-weight: 600;
}

.button {
  display: flex;
  margin-top: 20px;
  justify-content: center;
  gap: 20px;
  
  .el-button {
    padding: 12px 30px;
    font-size: 16px;
  }
}

.el-form-item {
  margin-bottom: 25px;
  
  .el-input {
    .el-input__inner {
      height: 45px;
      line-height: 45px;
      border-radius: 8px;
      padding-left: 45px;
      font-size: 15px;
      
      &:focus {
        border-color: #409EFF;
        box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
      }
    }
    
    .el-input__prefix {
      left: 15px;
      font-size: 18px;
    }
  }
}
</style>
